<?php

namespace App\Console\Commands\Fix;

use App\Console\BaseCommand;
use App\Consts\GlobalConst;
use App\Models\SysArticle;
use App\Models\SysComment;
use App\Models\SysDynamic;
use App\Models\SysNote;
use App\Models\SysReply;
use App\Models\UserMessageCommentReceive;
use App\Models\UserMessageCommentSend;
use App\Models\UserMessageLikeReceive;
use Illuminate\Database\Query\Builder;

/**
 * 消息修复脚本
 * Class CollectFixCommand
 * @package App\Console\Commands\Fix
 */
class MessageFixCommand extends BaseCommand
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fix:message {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'fix message status';

    /**
     * 删除状态 - 发出的评论
     * @return int
     */
    public function commentSend()
    {
        $subMode = $this->_getCommentSendSubModel();
        $finish = $subMode->count();
        // 每 100 单位合并处理一次
        $subMode->chunkById(100, function ($list) use ($finish) {
            foreach ($list as $item) {
                static $index = 1;
                $id = $item->message_id;
                $uid = $item->sender_id;
                $targetId = $item->target_id;
                $targetType = $item->target_type;
                // 获取被删除的主体信息
                $data = $this->_getDeleteTargetInfo($targetType, $targetId);
                if ($data) {
                    UserMessageCommentSend::getModel($uid)->where('message_id', $id)->update([
                        'is_deleted' => GlobalConst::IS_DELETED
                    ]);
                    $tableName = UserMessageCommentSend::getTableNameById($uid);
                    $info = "[$index/$finish] update `$tableName` delete status [comment/send] - message_id - " . $id;
                } else {
                    $info = "[$index/$finish] check message [comment/send] - message_id - " . $id;
                }
                // log info
                $this->info($info);
                if ($data) $this->logger($info, false, 'script');
                $index++;
            }
        }, 'message_id');
        return 1;
    }

    /**
     * 删除状态 - 收到的评论
     * @return int
     */
    public function commentReceive()
    {
        $subMode = $this->_getCommentReceiveSubModel();
        $finish = $subMode->count();
        // 每 100 单位合并处理一次
        $subMode->chunkById(100, function ($list) use ($finish) {
            foreach ($list as $item) {
                static $index = 1;
                $id = $item->message_id;
                $uid = $item->receive_id;
                $targetId = $item->target_id;
                $targetType = $item->target_type;
                // 获取被删除的主体信息
                $data = $this->_getDeleteTargetInfo($targetType, $targetId);
                if ($data) {
                    UserMessageCommentReceive::getModel($uid)->where('message_id', $id)->update([
                        'is_deleted' => GlobalConst::IS_DELETED
                    ]);
                    $tableName = UserMessageCommentReceive::getTableNameById($uid);
                    $info = "[$index/$finish] update `$tableName` delete status [comment/receive] - message_id - " . $id;
                } else {
                    $info = "[$index/$finish] check message [comment/receive] - message_id - " . $id;
                }
                // log info
                $this->info($info);
                if ($data) $this->logger($info, false, 'script');
                $index++;
            }
        }, 'message_id');
        return 1;
    }

    /**
     * 删除状态 - 收到的点赞
     * @return int
     */
    public function likeReceive()
    {
        $subMode = $this->_getLikeReceiveSubModel();
        $finish = $subMode->count();
        // 每 100 单位合并处理一次
        $subMode->chunkById(100, function ($list) use ($finish) {
            foreach ($list as $item) {
                static $index = 1;
                $id = $item->message_id;
                $uid = $item->receive_id;
                $targetId = $item->target_id;
                $targetType = $item->target_type;
                // 获取被删除的主体信息
                $data = $this->_getDeleteTargetInfo($targetType, $targetId);
                if ($data) {
                    UserMessageLikeReceive::getModel($uid)->where('message_id', $id)->update([
                        'is_deleted' => GlobalConst::IS_DELETED
                    ]);
                    $tableName = UserMessageLikeReceive::getTableNameById($uid);
                    $info = "[$index/$finish] update `$tableName` delete status [like/receive] - message_id - " . $id;
                } else {
                    $info = "[$index/$finish] check message [like/receive] - message_id - " . $id;
                }
                // log info
                $this->info($info);
                if ($data) $this->logger($info, false, 'script');
                $index++;
            }
        }, 'message_id');
        return 1;
    }

    /**
     * 获取被删除的主体信息
     * @param $targetType
     * @param $targetId
     * @return array|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed|object|null
     */
    private function _getDeleteTargetInfo($targetType, $targetId)
    {
        switch ($targetType) {
            case GlobalConst::TARGET_TYPE_ARTICLE :
                $data = SysArticle::getModel($targetId)
                    ->select(['id'])
                    ->where('article_id', $targetId)
                    ->where('is_deleted', GlobalConst::IS_DELETED)
                    ->first();
                break;
            case GlobalConst::TARGET_TYPE_NOTE :
                $data = SysNote::getModel($targetId)
                    ->select(['id'])
                    ->where('note_id', $targetId)
                    ->where('is_deleted', GlobalConst::IS_DELETED)
                    ->first();
                break;
            case GlobalConst::TARGET_TYPE_COMMENT :
                $data = SysComment::getModel($targetId)
                    ->select(['id'])
                    ->where('comment_id', $targetId)
                    ->where('is_deleted', GlobalConst::IS_DELETED)
                    ->first();
                break;
            case GlobalConst::TARGET_TYPE_REPLY :
                $data = SysReply::getModel($targetId)
                    ->select(['id'])
                    ->where('reply_id', $targetId)
                    ->where('is_deleted', GlobalConst::IS_DELETED)
                    ->first();
                break;
            case GlobalConst::TARGET_TYPE_DYNAMIC :
                $data = SysDynamic::getModel($targetId)
                    ->select(['id'])
                    ->where('dynamic_id', $targetId)
                    ->where('is_deleted', GlobalConst::IS_DELETED)
                    ->first();
                break;
            default :
                $data = [];
                break;
        }
        return $data;
    }

    /**
     * get sub model
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed
     */
    private function _getCommentReceiveSubModel()
    {
        $subMode = UserMessageCommentReceive::getSubModel(function (Builder $query) {
            $query->where('is_deleted', GlobalConst::IS_NOT_DELETE);
        }, ['id', 'message_id', 'target_id', 'target_type', 'receive_id', 'sender_id']);
        return $subMode;
    }

    /**
     * get sub model
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed
     */
    private function _getCommentSendSubModel()
    {
        $subMode = UserMessageCommentSend::getSubModel(function (Builder $query) {
            $query->where('is_deleted', GlobalConst::IS_NOT_DELETE);
        }, ['id', 'message_id', 'target_id', 'target_type', 'receive_id', 'sender_id']);
        return $subMode;
    }

    /**
     * get sub model
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed
     */
    private function _getLikeReceiveSubModel()
    {
        $subMode = UserMessageLikeReceive::getSubModel(function (Builder $query) {
            $query->where('is_deleted', GlobalConst::IS_NOT_DELETE);
        }, ['id', 'message_id', 'target_id', 'target_type', 'receive_id', 'sender_id']);
        return $subMode;
    }

}
